package com.ews.onlineexchange.controller;

import java.text.NumberFormat;
import java.util.List;

import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.Query;

import com.ews.onlineexchange.model.Sector;
import com.ews.onlineexchange.service.EntityHome;

@Named
@ConversationScoped
public class SectorHome extends EntityHome<Sector, String> {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private static NumberFormat nf;
	static {
		nf = NumberFormat.getInstance();
		nf.setMaximumFractionDigits(0);
		nf.setMinimumIntegerDigits(4);
		nf.setGroupingUsed(false);
	}
	
	@Inject
	private ParentSectorHome parentSectorHome;
	
	private Sector oldParent;
	
	private static final String QUERY_ROOT_SECTORS_EQL = ""
			+ "	FROM 	Sector sector"
			+ "	WHERE	sector.parentSector IS NULL ";
	
//	@Produces
	@Named
	public List<Sector> getRootSectorList() {
		return getEntityManager().createQuery(QUERY_ROOT_SECTORS_EQL).getResultList();
	}

	@Override
	public Sector loadInstance() {
		Sector loadInstance = super.loadInstance();
		if (loadInstance != null)
			this.setOldParent(loadInstance.getParentSector());
		return loadInstance;
	}

	@Override
	public String saveAndFlush() {
//		Sector newParent = this.getInstance().getParentSector();
		Sector newParent = parentSectorHome.isManaged() ? parentSectorHome.getInstance() : null;
		int nextIntSno = getNextIntSno(newParent);
		this.getInstance().setParentSector(newParent);
		
		getInstance().setIntSno(nextIntSno);
		getInstance().setSno(getNextSno(getInstance().getIntSno(), getInstance().getParentSector()));
		
		return super.saveAndFlush();
	}

	private static final String QUERY_MAX_SNO_EQL = ""
			+ "	SELECT		MAX(sector.intSno)	"
			+ "	FROM		Sector sector"
			+ "	WHERE		sector.parentSector = :parentSector ";
	private static final String QUERY_MAX_ROOT_SNO_EQL = ""
			+ "	SELECT		MAX(sector.intSno) "
			+ "	FROM		Sector sector"
			+ "	WHERE		sector.parentSector IS NULL	";
	
	private int getNextIntSno(Sector parentSector) {
		Query query = getEntityManager().createQuery(parentSector == null ? QUERY_MAX_ROOT_SNO_EQL : QUERY_MAX_SNO_EQL);
		if (parentSector != null)
			query.setParameter("parentSector", parentSector);
		Integer maxSno = (Integer) query.getSingleResult();
		if (maxSno == null)
			maxSno = 0;
		int nextSno = maxSno + 1;
		return nextSno;
	}
	
	private String getNextSno(int nextSno, Sector parentSector) {
		String result = nf.format(nextSno);
		if (parentSector != null)
			result = parentSector.getSno().concat(result);
		return result;
	}

	public Sector getOldParent() {
		return oldParent;
	}

	public void setOldParent(Sector oldParent) {
		this.oldParent = oldParent;
	}
	
	private static final String QUERY_ALL_ROOT_SECTORS_EQL = ""
			+ "	FROM 	Sector sector ORDER BY sno,createDate DESC";
	
	@Named
	public List<Sector> getAllSectorList() {
		return getEntityManager().createQuery(QUERY_ALL_ROOT_SECTORS_EQL).getResultList();
	}
}
